package com.athuiju.fenxiaoback.controller;


import com.athuiju.fenxiaoback.entity.TbCategory;
import com.athuiju.fenxiaoback.entity.TbSpu;
import com.athuiju.fenxiaoback.entity.vo.TbSpuQuery;
import com.athuiju.fenxiaoback.entity.vo.TbSpuQueryByCid2;
import com.athuiju.fenxiaoback.service.GoodsService;
import com.athuiju.fenxiaoback.service.TbCategoryService;
import com.athuiju.fenxiaoback.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品类目表，类目和商品(spu)是一对多关系，类目与品牌是多对多关系 前端控制器
 * </p>
 *
 * @author athuiju
 * @since 2021-06-30
 */
@Api(description = "nt分类",tags = "nt分类")
//@CrossOrigin
@RestController
@RequestMapping("/fenxiaoback01/tb-category")
public class TbCategoryController {
    @Autowired
    private TbCategoryService categoryService;
    @Autowired
    private GoodsService goodsService;



    @ApiOperation(value = "根据分类id查询商品---分页查询")
    @PostMapping("spuconditionbycid")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = TbSpu.class),
    })
    public R pageQueryCondition(
            @ApiParam(name = "goodsQuery", value = "查询对象", required = false)
            @RequestBody TbSpuQueryByCid2 goodsQuery){
        Long current = goodsQuery.getCurrent();
        Long limit = goodsQuery.getLimit();

        //创建Page对象
        Page<TbSpu> page =new Page<>(current,limit);
        //调用Service
        goodsService.pageQueryByCid3(page, goodsQuery);
        //得到当前页数
        List<TbSpu> records = page.getRecords();
        if (records==null){
            return R.error().put("msg","没有找到任何商品");
        }

        return R.ok().put("data",records);
    }





    @ApiOperation(value = "查询子分类")
    @GetMapping("getCategoriesByCid/{cid}")
    public R getCategoriesByCid(@PathVariable("cid") String cid) {
        QueryWrapper<TbCategory> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id",cid);

        List<TbCategory> list = categoryService.list(wrapper);
        return R.ok().put("list",list);
    }


    /**
     * 查询所有分类
     */
    @ApiOperation(value = "查询分类")
    @GetMapping("categories")
    public R getAllCate() {
        List<TbCategory> cateLists = categoryService.queryAllCate();
        return R.ok().put("cateLists", cateLists).put("total", cateLists.size());
    }








}

